﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Business;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class BasicEnvironmentStatusController : BaseController
    {
        private ISiteBranch siteBranch;
        private IUser_X user;
        private IBusinessSystem businessSystem;
        protected override void Dispose(bool disposing)
        {
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)user).Dispose();
            ((IDisposable)businessSystem).Dispose();
        }

        [InjectionConstructor]
        public BasicEnvironmentStatusController(ISiteBranch siteBranch, IUser_X user, IBusinessSystem businessSystem)
        {
            this.siteBranch = siteBranch;
            this.user = user;
            this.businessSystem = businessSystem;
        }

        //是否是传SiteID 和 服务的名字比较好 。
        //关于服务器 中间件等 就在新页面打开，不要再无端的增加复杂性。
        //<a href="@Url.Action("about", new {chinese="中更"})">Hello Chinees</a>
        public ActionResult Index(int businessSystemId)
        {
            base.SetRoles(user);
            var bSystem = businessSystem.GetBusinessSystemByID(businessSystemId);
            if (!UserSiteIDs.Contains(bSystem.SiteID)) return new HttpStatusCodeResult(403);

            List<SiteBranchModel> sites = siteBranch.GetSiteALLBranch().ToList();
            var sitesContainsTheBS = businessSystem.GetSiteIDsContainsTheBusinessSystem(bSystem.Name);

            List<decimal> siteIDsContainsTheBS = sitesContainsTheBS.Select(o => o.SiteID).ToList();

            ViewBag.Sites = (from c in sites
                             where UserSiteIDs.Contains(c.ID) && siteIDsContainsTheBS.Contains(c.ID)
                             select c).ToList();

            ViewBag.SiteID = bSystem.SiteID;

            ViewData["ServiceName"] = bSystem.Name;
            ViewData["ServiceAlias"] = bSystem.Alias;
            ViewData["ServiceID"] = bSystem.ID;
            return View();
        }

        //public ActionResult Index(int id)
        //{
        //    base.SetRoles(user);
        //    List<SiteBranchModel> sites = siteBranch.GetSiteALLBranch().ToList();

        //    var bSystem = businessSystem.GetBusinessSystemByID(id);
        //    ViewData["ServiceName"] = bSystem.Alias;
        //    ViewData["ServiceID"] = bSystem.ID;
        //    return View();
        //}
    }
}
